
CV.Tree.implement({

    initSelection: function() {
        this.defaults.selectClass = '';
        this.wrapper.addEvent('mousedown', this.attachSelect.bind(this));
    },
    attachSelect: function() {
        if(!['icon', 'name', 'node'].contains(this.mouse.target)) return;
        var node = this.mouse.node;
        if(!node) return;
        this.select(node);
    },
    select: function(node) {
        if(!node) return this;
        var current = this.selected;
        if(current == node) return this;
        if(current) {
            current.select(false);
            this.fireEvent('unSelect', [current]);
            this.fireEvent('selectChange', [current, false]);
        }
        this.selected = node;
        node.select(true);
        this.fireEvent('select', [node]);
        this.fireEvent('selectChange', [node, true]);
        return this;
    },
    unselect: function() {
        var current = this.selected;
        if(!current) return this;
        this.selected = false;
        current.select(false);
        this.fireEvent('unSelect', [current]);
        this.fireEvent('selectChange', [current, false]);
        return this;
    },
    getSelected: function() {
        return this.selected;
    },
    isSelected: function(node) {
        return node.isSelected();
    }
});

CV.Tree.Node.implement({

    select: function(state) {
        this.state.selected = state;
        if(!CV.Tree.Draw.isUpdatable(this)) return;
        var wrapper = this.getDOM('wrapper');
        wrapper[(state ? 'add' : 'remove') + 'Class'](this.selectClass || 'cv-tree-node-selected');
    },
    isSelected: function() {
        return this.state.selected;
    }
});
